<template>
  <div class="app-container">

    <!-- 获取结算记录列表 -->
    <div>

      <div style="display: flex;flex-wrap: nowrap;">
        <div>
          <el-form>
            <el-row class="rowFormItem">
              <!--<el-col :span="12" style="margin-right: 10px;">
                <el-input v-model="form.keyword" placeholder="订单号/下单用户/代理人用户昵称" />
              </el-col>-->
              <el-col :span="2">
                <el-form-item style="text-align: left;width: 500px;">
                 <!-- <el-button type="primary" size="large" @click="handleSubmit()">检索</el-button>
                  <el-button type="warning" size="large" style="margin-left: 8px" @click="handleReset()">重置</el-button>-->
                  <el-button type="primary" size="large" style="margin-left: 8px" @click="showDialog()">添加结算记录</el-button>

                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>

      </div>

      <div class="tableClass">
        <el-table :data="formData" v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                  border style="width: 100%;">
          <el-table-column align="center" prop="agent_info.realname" label="代理人" />
          <el-table-column align="center" prop="agent_info.type_name" label="代理人类型" />
          <el-table-column align="center" prop="agent_info.phone" label="手机号" />
          <el-table-column align="center" prop="amount" label="结算金额"></el-table-column>
<!--          <el-table-column align="center" prop="" label="结算性质" />-->
          <el-table-column align="center" prop="created_at" label="创建记录时间" />
          <!--<el-table-column align="center" label="操作" width="300">
            <template slot-scope="scope">
              <el-button v-if="scope.row.review == '0'" size="mini" type="primary" @click="confirmOrderReview(scope.row.id,'pass')">通过</el-button>
              <el-button v-if="scope.row.review == '0'" size="mini" type="danger" @click="confirmOrderReview(scope.row.id,'refuse')">拒绝</el-button>
              <el-button size="mini" type="success" v-if="scope.row.is_pay=='1' && scope.row.is_refund == 0" @click="orderRefund(scope.row.order_num)">退款</el-button>
              <el-button size="mini" type="info" v-if="scope.row.is_pay=='1' && scope.row.is_refund == 1">已退款</el-button>
            </template>
          </el-table-column>-->
        </el-table>
        <br>
        <!-- 分页 -->
        <div style="text-align: center;">
          <el-pagination background layout="prev, pager, next" @current-change="pageChange" :current-page="page" :page-size="limit"
                         :total="totalPage" />
        </div>
      </div>

    </div>

    <el-dialog :visible.sync="dialogShow" title="添加结算记录">
      <div>
        <el-form ref="form" :model="addForm" label-width="140px">
          <el-form-item label="代理人" :required="true">
            <el-select style="width: 300px" filterable clearable v-model="addForm.agent_user_id" placeholder="请选择一级代理人">
              <el-option v-for="(item,index) in agent_list" :key="index" :label="`${item.realname}（账户余额：${item.balance}）`" :value="item.user_id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="结算金额/元" :required="true">
            <el-input style="width: 300px" type="number" v-model="addForm.amount" placeholder="请输入手机号码"></el-input>
          </el-form-item>
        </el-form>
        <div style="text-align: center">
          <el-button style="margin: auto" type="success" @click="addAgentSettleAccountsRecord()">设置</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: "settleRecordList",
    data() {
      return {
        page: 1,
        limit: 15,
        formData: [],
        loading: false,
        agent_info: null,
        totalPage: 0,
        form: {
          keyword: "",
        },
        settle_nature: [
          {
            id: 1,
            name: "后台创建"
          },
          {
            id: 2,
            name: "用户申请提现"
          }
        ],
        dialogShow: false,
        addForm: {
          agent_user_id: '',
          amount: 0
        },
        agent_list: []
      }
    },

    mounted() {
      this.getAgentSettleAccountsRecordList()
      this.getAgentList()
    },

    methods: {

      showDialog() {
        this.addForm.agent_user_id = ''
        this.addForm.amount = 0
        this.dialogShow = true
      },

      async addAgentSettleAccountsRecord() {
        if (!this.addForm.agent_user_id) {
          this.$message.warning("请选择代理人")
          return
        }
        const res = await this.$api.addAgentSettleAccountsRecord(this.addForm);
        if (res.data.level == 'success') {
          this.$message.success(res.data.message)
          this.dialogShow = false
          this.getAgentSettleAccountsRecordList()
        } else {
          this.$message.error(res.data.message)
        }

      },

      async getAgentList() {
        const res = await this.$api.getAgentList()
        if (res.data.level == 'success') {
          this.agent_list = res.data.data
        }
      },


      // 检索
      handleSubmit() {
        this.page = 1
        this.getAgentSettleAccountsRecordList()
      },

      // 重置
      handleReset() {
        this.form.keyword = ""
        this.page = 1
        this.getAgentSettleAccountsRecordList()
      },

      async getAgentSettleAccountsRecordList() {
        this.loading = true;
        const params = {
          page: this.page,
          limit: this.limit,
          key: this.form.keyword
        }
        const res = await this.$api.getAgentSettleAccountsRecordList(params);
        if (res.data.level === 'success') {
          this.formData = res.data.data;
          this.agent_info = this.formData.agent_info
        } else {
          this.$message.error(res.data.message);
        }
        this.loading = false;
      },

      pageChange(e) {
        this.page = e;
        this.getAgentSettleAccountsRecordList()
      },

    },
  }
</script>

<style scoped>

</style>
